package com.thanhnx.tlg1;

import java.util.ArrayList;

import adapter.ColAAAdapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

public class ActivityTest extends Activity {
	private LinkListView listview;
	private ColAAAdapter adapter;
	private MyApp myApp;
	private MyAdapter myAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		myApp = (MyApp) this.getApplicationContext();

		listview = (LinkListView) findViewById(R.id.colA_2);
		listview.setItemsCanFocus(true);
		listview.setVerticalScrollBarEnabled(false);

		// adapter = new ColAAAdapter(getApplicationContext(), myApp.colA);
		myAdapter = new MyAdapter();
		listview.setAdapter(myAdapter);

	}

	public class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		public ArrayList myItems = new ArrayList();

		public MyAdapter() {
			mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			for (int i = 0; i < 500; i++) {
				ListItem listItem = new ListItem("Caption" + i);
				myItems.add(listItem);
			}
			notifyDataSetChanged();
		}

		public int getCount() {
			return myItems.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.item_col_a, null);
				holder.caption = (EditText) convertView
						.findViewById(R.id.item_col_a);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			// Fill EditText with the value you have in data source
			holder.caption.setText(((ListItem) myItems.get(position)).caption);
			holder.caption.setId(position);

			// we need to update adapter once we finish with editing
			holder.caption
					.setOnFocusChangeListener(new OnFocusChangeListener() {
						public void onFocusChange(View v, boolean hasFocus) {
							if (!hasFocus) {
								final int position = v.getId();
								final EditText Caption = (EditText) v;
								myItems.set(position, new ListItem(Caption
										.getText().toString()));
							}
						}
					});

			return convertView;
		}
	}

	class ViewHolder {
		EditText caption;
	}

	class ListItem {
		String caption;

		public ListItem(String string) {
			caption = string;
		}

		public String getCaption() {
			return caption;
		}

		public void setCaption(String cap) {
			caption = cap;
		}
	}

}
